package com.vhall.component.service.room.event.handler.document;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.vhall.component.dao.room.RoomDocumentsMapper;
import com.vhall.component.entity.room.RoomDocumentsEntity;
import com.vhall.component.framework.common.exception.BusinessException;
import com.vhall.component.plugin.common.exception.BizErrorCode;
import com.vhall.component.plugin.common.utils.algorithm.PaasCallbackMd5SignValidator;
import com.vhall.component.plugin.core.service.PaasService;
import com.vhall.component.service.room.event.handler.AbstractEventHandler;
import com.vhall.component.service.room.event.handler.EventHandlerType;
import com.vhall.component.service.room.event.handler.RedirectUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Map;

import static com.vhall.component.service.room.event.EventConstant.DOCUMENT_TRANS_OVER;

/**
 * 文档转码成功后回调通知
 *
 * @author jingwen.li
 * @date 2023/2/16
 */
@Slf4j
@Component
@EventHandlerType(DOCUMENT_TRANS_OVER)
public class DocumentTransOverEventHandler extends AbstractEventHandler {

    @Autowired
    private RoomDocumentsMapper roomDocumentsMapper;
    @Autowired
    private PaasService paasService;
    @Autowired
    private RedirectUtil redirectUtil;
    @Value("${old.callback.document.url}")
    private String callbackUrl;
    @Override
    public void handle(Map<String, Object> callBack) {
        redirectUtil.redirect(callbackUrl, callBack);
//        event	string	是	document/trans-over
//        refer	string	是	vhall
//        time	string	是	回调时间戳
//        document_id	string	是	文档ID
//        status	int	是	1 转码成功 2 转码失败
//        signature	string	是	回调签名
        log.info("收到文档转码完成事件：{}, 事件详情：{}", DOCUMENT_TRANS_OVER, callBack);
        try {
            String documentId = (String) callBack.get("document_id");
            if (StrUtil.isBlank(documentId)) {
                log.error("收到文档转码完成事件-{},文档ID为空", DOCUMENT_TRANS_OVER);
            }
            LambdaQueryWrapper<RoomDocumentsEntity> wrapper = Wrappers.lambdaQuery();
            wrapper.eq(RoomDocumentsEntity::getDocumentId, documentId);
            RoomDocumentsEntity entity = roomDocumentsMapper.selectOne(wrapper);
            if (entity == null) {
                throw new BusinessException(BizErrorCode.EMPTY_DOCUMENT);
            }

            //PaaS端获取文档信息
            JSONObject file = paasService.documentInfo(documentId);
            //获取PaaS响应参数
            Integer transStatus = file.getInteger("trans_status");
            Integer page = file.getInteger("page");
            String statusJpeg = String.valueOf(file.getInteger("status_jpeg"));
            String statusSwf = file.getString("status_swf");
            String status = String.valueOf(file.getInteger("status"));

            entity.setTransStatus(transStatus);
            entity.setPage(page);
            entity.setStatusJpeg(statusJpeg);
            entity.setStatusSwf(statusSwf);
            entity.setStatus(status);
            entity.setUpdatedAt(LocalDateTime.now());

            roomDocumentsMapper.updateById(entity);
        } catch (Exception e) {
            log.error("更新文档状态失败：{}", e.getMessage());
        }
    }


}
